package com.gdut.simple.model.impl;

import com.gdut.simple.common.LogUtils;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.model.ILoginModel;

import cn.bmob.v3.exception.BmobException;
import rx.Subscriber;

public class LoginModelImpl implements ILoginModel {

    @Override
    public void login(String username, String password, final RequestCallback<User> callback) {
        final User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.loginObservable(User.class).subscribe(new Subscriber<User>() {
            @Override
            public void onCompleted() {
                LogUtils.d("---login complete---");
            }

            @Override
            public void onError(Throwable throwable) {
                LogUtils.e(new BmobException(throwable).getMessage());
                if (callback != null) {
                    callback.onFailure(new BmobException(throwable).getMessage());
                }
            }

            @Override
            public void onNext(User user) {
                if (callback != null) {
                    callback.onSuccess(user);
                }
            }
        });
    }

}
